﻿Ext.ns('fifachamps');

fifachamps.login = {
    initialize: function() {
        var login_panel = new Ext.Panel({
            id: 'PNL_login',
            title: 'Login',
            layout: 'form',
            renderTo: 'DIV_login',
            border: true,
            autoHeight: true,
            bodyPadding: 15,
            items: [
                {
                    xtype: 'textfield',
                    id: 'TXT_username',
                    fieldLabel: 'Username',
                    allowBlank: false,
                    emptyText: 'Username'
                }, {
                    xtype: 'textfield',
                    id: 'TXT_password',
                    inputType: 'password',
                    allowBlank: false,
                    fieldLabel: 'Password',
                    emptyText: 'Password'
                }
            ], buttonAlign: 'center'
            , buttons: [
                {   id: 'BTN_login', formBind: true, text: 'Login', handler: function(btn, evt) { 
                        var txt_username = Ext.getCmp('TXT_username');
                        var txt_password = Ext.getCmp('TXT_password');

                        if (txt_username.validate() && txt_password.validate()) {
                            fifachamps.login.login(txt_username.getValue(), txt_password.getValue());                
                        } else {
                            Ext.Msg.alert('Login', 'Please provide a username and password.');
                        }
                    } 
                }
            ]
        });
    }, 
    login: function(username, password) {
        //mask
        Ext.get('DIV_page').mask("Loading");
        //post
        Ext.Ajax.request({
            url: 'Proxy.ashx',
            reader: 'json',
            method: 'POST',
            success: function(result) { 
               var ob = Ext.decode(result.responseText);
               
               if (ob.success) {
                    window.location.href = ob.message;                    
               } else {
                    Ext.Msg.alert('Login', ob.message);
               }
               Ext.get('DIV_page').unmask();
            },          
            params: { 
                cmd: 'login_user',
                username: username,
                password: password
            }
        });
    }
}

Ext.onReady(function() {
    fifachamps.login.initialize();
});